At the same time, human rights activists say members of the Rio police force, eager to clean up the city ahead of the Aug. 5 opening ceremony for the Games, have shot dead more than 100 people this year, most of them young black men living in poor neighborhoods.

But advocates say the constant homophobic violence also threatens to upend an idealized national ethos that promises equality and respect for all Brazilians.

“We live off this image as an open and tolerant place,” said Jandira Queiroz, the mobilization coordinator at Amnesty International Brazil. “Homophobic violence has hit crisis levels, and it’s getting worse.”

Brazil’s near-mythic reputation for tolerance is not without justification. In the nearly three decades since democracy replaced military dictatorship, the Brazilian government has introduced numerous laws and policies aimed at improving the lives of sexual minorities. In 1996, it was among the first to offer free antiretroviral drugs to people with H.I.V. In 2003, Brazil became the first country in Latin America to recognize same-sex unions for immigration purposes, and it was among the earliest to allow gay couples to adopt children.

In 2013, the Brazilian judiciary effectively legalized same-sex marriage.

Some experts suggest that liberal government policies may have gotten too far ahead of traditional social mores. The anti-gay violence, they contend, can be traced to Brazil’s culture of machismo and a brand of evangelical Christianity, exported from the United States, that is outspoken in its opposition to homosexuality.

Evangelicals make up nearly a quarter of Brazil’s population, up from 5 percent in 1970, and religious leaders reach millions of people through the hundreds of television and radio stations they have purchased in recent years.